import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_select_time_period.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/ext.dart';
import 'package:enterprise_v6_app/app/data/entities/application/Inventory_system/measures_task_record_list_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/inventory_system/risk_control_checklist/control_situation/control_situation_controller.dart';

///管控情况
class ControlSituationPage extends StatelessWidget {
  ControlSituationPage({super.key});

  final controller = Get.put(ControlSituationController());


  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            key: controller.riskRightScaffoldKey,
            backgroundColor: AppColors.bg_color_grey,
            appBar: CustomAppBar(
              titleText: '历史执行情况',
            ),
            body: Padding(
              padding: EdgeInsets.only(left: 16.w, right: 16.w, top: 12.w),
              child: Column(
                children: [
                  Row(
                    children: [
                      Expanded(
                        child: CustomSelectTimePeriod(
                          onConfirm: (startTime, endTime) {
                            controller.startTime = startTime;
                            controller.endTime = endTime;
                            controller.refreshPage();
                          },
                        ),
                      ),
                      AppGaps.hGap8,
                      _screen(),
                      AppGaps.vGap10,
                    ],
                  ),
                  AppGaps.vGap14,
                  Expanded(
                      child: CustomEasyRefreshList<MeasuresTaskRecordListEntity>(
                        key: controller.globalKey,
                        dataLoad: controller.measuresTaskRecordList,
                        itemBuilder: (item, index) {
                          return CustomTitleChartPage(
                              title: '',
                              titleRightWidget: _getResult('${item.checkStatus}'),
                              child: Column(
                                children: [
                                  Row(
                                    children: [
                                      Text(
                                        '履责执行期限：',
                                        style: TextStyle(fontSize: 13.sp, fontWeight: FontWeight.w400, color: const Color(0xffA5A6B5)),
                                      ),
                                      const Spacer(),
                                      Text(
                                        item.checkTime ?? '',
                                        style: TextStyle(fontSize: 12.sp, fontWeight: FontWeight.w400, color: const Color(0xff495061)),
                                      ),
                                    ],
                                  ),
                                  AppGaps.vGap10,
                                  Row(
                                    children: [
                                      Text(
                                        '执行人：',
                                        style: TextStyle(fontSize: 13.sp, fontWeight: FontWeight.w400, color: const Color(0xffA5A6B5)),
                                      ),
                                      const Spacer(),
                                      Text(
                                        item.checkUserName ?? '',
                                        style: TextStyle(fontSize: 12.sp, fontWeight: FontWeight.w400, color: const Color(0xff495061)),
                                      ),
                                    ],
                                  ),
                                  AppGaps.vGap10,
                                  Row(
                                    children: [
                                      Text(
                                        '执行时间：',
                                        style: TextStyle(fontSize: 13.sp, fontWeight: FontWeight.w400, color: const Color(0xffA5A6B5)),
                                      ),
                                      const Spacer(),
                                      Text(
                                        item.checkTime ?? '',
                                        style: TextStyle(fontSize: 12.sp, fontWeight: FontWeight.w400, color: const Color(0xff495061)),
                                      ),
                                    ],
                                  ),
                                  AppGaps.vGap10,
                                  Row(
                                    children: [
                                      Text(
                                        '执行结果：',
                                        style: TextStyle(fontSize: 13.sp, fontWeight: FontWeight.w400, color: const Color(0xffA5A6B5)),
                                      ),
                                      const Spacer(),
                                      Text(
                                        '',
                                        style: TextStyle(fontSize: 12.sp, fontWeight: FontWeight.w400, color: const Color(0xff495061)),
                                      ),
                                    ],
                                  ),
                                  AppGaps.vGap10,
                                ],
                              ).clickOnce(() {
                                controller.clickDetail(item);
                              }));
                        },
                      ))
                ],
              ),
            ),
            endDrawer: Container(
              width: AppDimens.sw - 100.w,
              height: AppDimens.sh,
              padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 55.w),
              decoration: const BoxDecoration(
                  gradient: LinearGradient(
                    begin: Alignment.topCenter,
                    end: Alignment.bottomCenter,
                    colors: [
                      Color(0xFFE7EAFF),
                      Color(0xFFFFFFFF),
                      Color(0xFFFFFFFF),
                    ],
                  )),
              child: Column(
                children: [
                  Expanded(
                      child: SingleChildScrollView(
                        child: Column(
                          children: [
                            Align(
                              alignment: Alignment.centerLeft,
                              child: Text(
                                '筛选',
                                style: TextStyle(
                                  fontSize: 18.sp,
                                  fontWeight: FontWeight.w500,
                                ),
                              ),
                            ),
                            AppGaps.vGap20,
                            _selectType('状态'),
                          ],
                        ),
                      )),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: [
                      OutlinedButton(
                        onPressed: controller.reset,
                        child: Text(
                          '重置',
                          style: TextStyle(fontSize: 16.sp, color: AppColors.font_color_blue),
                        ),
                      ),
                      ElevatedButton(
                          style: ButtonStyle(backgroundColor: MaterialStateProperty.all(AppColors.font_color_blue)),
                          onPressed: controller.confirm,
                          child: Text(
                            '查询',
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: 16.sp,
                            ),
                          )),
                    ],
                  ),
                ],
              ),
            ),
          );
        });
  }

  Widget _selectType(String firstName, {Widget? input}) {
    return Column(
      children: [
        Align(
          alignment: Alignment.centerLeft,
          child: Text(
            firstName,
            style: TextStyle(
              fontSize: 14.sp,
              fontWeight: FontWeight.w400,
              color: const Color(0xFF262F38),
            ),
          ),
        ),
        AppGaps.vGap16,
        input ??
            Container(
              height: 38.w,
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(8),
                border: Border.all(width: 1.w, color: const Color(0xFFE3E7EE)),
              ),
              child: Row(
                children: [
                  AppGaps.hGap20,
                  Expanded(
                    child: Text(
                      controller.implementationStatus,
                      style: TextStyle(
                        fontSize: 12.sp,
                        color: const Color(0xFFA0A7B5),
                        fontWeight: FontWeight.w400,
                      ),
                    ),
                  ),
                  SvgPicture.asset(AppImages.icon_filter_right_arrow),
                  AppGaps.hGap20,
                ],
              ),
            ).clickOnce(() {
              if (firstName == '状态') {
                controller.selectCurrencyDangerLevelType();
              }
            }),
        AppGaps.vGap16,
      ],
    );
  }

  /// 正常：0；存在隐患：1；未排查：2；逾期排查：3  ---除了2都是已完成 2是逾期
  Widget _getResult(String? i) {
    switch (i) {
      case '2':
        return _title('逾期', Colors.redAccent);
      default:
        return _title('已完成', Colors.lightGreen);
    }
  }

  Widget _title(String title, Color color) {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 2.w, horizontal: 11.w),
      decoration: BoxDecoration(
          color: color,
          borderRadius: const BorderRadius.only(
            topLeft: Radius.circular(10),
            topRight: Radius.circular(10),
            bottomRight: Radius.circular(10),
          )),
      child: Text(
        title,
        style: TextStyle(
          color: Colors.white,
          fontSize: 10.sp,
          fontWeight: FontWeight.w400,
        ),
      ),
    );
  }

  ///筛选
  Widget _screen() {
    return Container(
      width: 36.w,
      height: 36.w,
      alignment: Alignment.center,
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(8.w)),
      child: SvgPicture.asset(
        AppImages.icon_search_screen,
        width: 16.w,
      ),
    ).click(controller.onFilterButtonPressed);
  }
}
